
var gadgetServer = "http://localhost:3002";
var my_gadgets = [];


function init_gadgets(gadget_list) {
	my_gadgets = [];
	for (gadget in gadget_list) {
		create_gadget(gadget_list[gadget].spec_url, 
					  'default',  //container_id
					  gadget_list[gadget].title);
	}	
	if (my_gadgets.length > 0) {
		requestGadgetMetaData(my_gadgets[0].url, 'profile');
	}
	else {
		alert('You have no gadgets to display');
	}
}
	
function handle_add_gadget() {
	dijit.byId('leftPane').toggle();
}

function create_gadget(spec_url, container_id, gadget_title) {
	var contentPane = dojo.byId('gadget_content_pane');
	contentPane.appendChild(build_gadget(container_id, gadget_title));
	alert('create3');
	my_gadgets.push(		
		{
			url:spec_url, 
			container_id:container_id,
			moduleId:1
		});
	alert('after push');
}

/*
        <div class="gadget">
            <div class="gadget_hdr">
                <div class="gadget_title" style="float:left;">Gadget 2</div>
				<div class="gadget_tools" style="float:right;">
					<%= image_tag('wrench_orange.png') %>
					<%= image_tag('cross.png') %>
				</div>
            </div>
            <div class="gadget_body">
                <div id="gadget-chrome" class="gadgets-gadget-chrome"></div>
            </div>
        </div>
 */
function build_gadget(container_id, gadget_title) {
	var gadgetEl = document.createElement('div');
	gadgetEl.className = 'gadget';
	
/*	var gadgetHdrEl = document.createElement('div');
	gadgetHdrEl.className = 'gadget_hdr';
	
	var gadgetTitleEl = document.createElement('div');
	gadgetTitleEl.className = 'gadget_title';
	gadgetTitleEl.setAttribute('id', 'gadget_title_' + container_id);
	gadgetTitleEl.appendChild(document.createTextNode(gadget_title));
	gadgetHdrEl.appendChild(gadgetTitleEl);
	
	var gadgetToolsEl = document.createElement('div');
	gadgetToolsEl.className = 'gadget_tools';
	
	var linkEl2 = document.createElement('a');
	linkEl2.setAttribute('href', 'javascript:remove_gadget()');
	var crossImgEl = document.createElement('img');
	crossImgEl.setAttribute('BORDER', '0');
	crossImgEl.style.border = 'none'
	crossImgEl.src = '/images/cross.png';
	linkEl2.appendChild(crossImgEl);
	gadgetToolsEl.appendChild(linkEl2);	
	gadgetHdrEl.appendChild(gadgetToolsEl);
	
	var linkEl = document.createElement('a');
	linkEl.setAttribute('href', 'javascript:configure_gadget()');
	var wrenchImgEl = document.createElement('img');
	wrenchImgEl.setAttribute('BORDER', '0');
	wrenchImgEl.style.border = 'none';
	wrenchImgEl.src = '/images/wrench_orange.png';
	linkEl.appendChild(wrenchImgEl);
	gadgetToolsEl.appendChild(linkEl);  */
	
	var gadgetBodyEl = document.createElement('div');
	gadgetBodyEl.className = 'gadget_body';
	
	var contentEl = document.createElement('div');
	contentEl.setAttribute('id', container_id);
	contentEl.className = 'gadgets-gadget-chrome';
	gadgetBodyEl.appendChild(contentEl);
	
//	gadgetEl.appendChild(gadgetHdrEl);
	gadgetEl.appendChild(gadgetBodyEl);
	
	return gadgetEl;
}

function add_gadget(gadget_id) {
	dojo.xhrPost({
	    url: 'dashboard/add_gadget?url=' + gadget_id,
	    timeout: 3000, // give up after 3 seconds
		load: function (data) {gadget_added(data);},
		error: function (error) {gadget_added_error(error);}
	});
}

function gadget_added(data) {
	document.getElementById('gadget_content_pane').innerHTML = '';
	//init_shindig();
	var gadget_list = eval(data);
	init_gadgets(gadget_list);
}

function gadget_added_error(error) {
	alert('ERROR: ' + dumpObj(error) );
}

function remove_gadget(gadget_id) {
	var gadget = gadgets.container.getGadget(gadget_id);
	var chrome = gadgets.container.layoutManager.getGadgetChrome(gadget);
	dojo.xhrPost({
	    url: 'http://localhost:3000/dashboard/remove_gadget?chrome_id=' + chrome.id,
	    timeout: 3000, // give up after 3 seconds
		load: function (data) {gadget_removed(data);},
		error: function (error) {gadget_removed_error(error);}
	});
}

function gadget_removed(data) {
	document.getElementById('gadget_content_pane').innerHTML = '';
	init_shindig();
	var gadget_list = eval(data);
	init_gadgets(gadget_list);
}

function gadget_removed_error(error) {
	alert('ERROR: ' + dumpObj(error) );
}

